// ActionScript file
import com.xsky.entity.CountData;
import com.xsky.entity.Workset;
import com.xsky.util.database.ModelManager;

import flash.events.MouseEvent;

import mx.controls.Alert;
import mx.core.Application;
import mx.events.DataGridEvent;

private var numberRegex:RegExp
public function initApp():void
{					 
	 btnLogin.addEventListener(MouseEvent.CLICK,btnLoginFunc);
}
public function btnLoginFunc(event:MouseEvent):void
{
	initDb();
	var username:String = txtUsername.text;
	var password:String = txtPassword.text;
	var user:CountData = new CountData();
	user.setPassword(password);
	user.setUserName(username);
	var loginResult:String = dbEntity.login(user);
	if(loginResult == "ok")
	{
		loginOk();
	}
	else
	{
		Alert.okLabel="确定";
		Alert.show("登录失败，请重新登录");
	}
	
}
public function loginOk():void
{
	Application.application.loginWindow.visible=false;
	Application.application.width = flash.system.Capabilities.screenResolutionX-100;
	Application.application.height = flash.system.Capabilities.screenResolutionY-100;
	Application.application.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
	titleWord.x=0;
	titleWord.y=0;
	titleWord.width=flash.system.Capabilities.screenResolutionX;
	titleWord.height=120;
	this.addChild(titleWord);
	
	xToolbar.x = 2;
	xToolbar.y = titleWord.height+2;
	xToolbar.width = flash.system.Capabilities.screenResolutionX - 2;
	xToolbar.height = 40;
	
	this.addChild(xToolbar);
	
	cmenuBar.x = 2;
	cmenuBar.y = titleWord.height+xToolbar.height+3;
	cmenuBar.width=200;
	cmenuBar.height = this.height - titleWord.height - xToolbar.height-10;
	this.addChild(cmenuBar);
	
	cmenuBar.btnAddWorkSet.addEventListener(MouseEvent.CLICK, openWorksetWindow);
	cmenuBar.btnInputWorkRecord.addEventListener(MouseEvent.CLICK, openInputWorkRecordWindow);
	cmenuBar.btnViewWorkRecord.addEventListener(MouseEvent.CLICK, openViewWorkRecordWindow);
	cmenuBar.btnWorkSeries.addEventListener(MouseEvent.CLICK, openStatisticsSearchWindow);
	
	workSpace.x = cmenuBar.width+5;
	workSpace.y = titleWord.height+xToolbar.height+3;
	workSpace.width = flash.system.Capabilities.screenResolutionX - cmenuBar.width-6;
	workSpace.height = cmenuBar.height;
	addChild(workSpace);
	
	xToolbar.btnExitWindow.addEventListener(MouseEvent.CLICK, exitApp);
	xToolbar.btnExitWindow.x = xToolbar.width-xToolbar.btnExitWindow.width-60;
	xToolbar.btnExitWindow.y = 8;
	
	worksetWindow.x = 50;
	worksetWindow.y = 10;
	worksetWindow.width=353;
	worksetWindow.height=516;
	worksetWindow.visible = false;
	workSpace.addChild(worksetWindow);
	
	worksetWindow.worksetGrid.editable=false;
	worksetWindow.worksetGrid.addEventListener(DataGridEvent.ITEM_EDIT_END, refreshData);
	//worksetWindow.worksetGrid.addEventListener(MouseEvent.CLICK, showData);
		
	worksetWindow.hello.addEventListener(MouseEvent.CLICK, showMe);
	addWorkset.x=worksetWindow.x + worksetWindow.width+10;
	addWorkset.y=100;
	addWorkset.visible=false;
	workSpace.addChild(addWorkset);
	addWorkset.btnOk.addEventListener(MouseEvent.CLICK, addWorkSet);
	
	modifyWorkset.x = worksetWindow.x+worksetWindow.width+20;
	modifyWorkset.y = 10;
	modifyWorkset.visible=false;
	workSpace.addChild(modifyWorkset);
	
	
	addWorkerWindow.x=100;
	addWorkerWindow.y=100;
	addWorkerWindow.visible = false;
	workSpace.addChild(addWorkerWindow);
	
	modifyWorker.visible=false;
	workSpace.addChild(modifyWorker);
	
	workersWindow.x = 100;
	workersWindow.y = 10;
	workersWindow.width=410;
	workersWindow.height=530;
	workersWindow.visible=false;
	workSpace.addChild(workersWindow);
	workersWindow.btnAddworker.addEventListener(MouseEvent.CLICK, showAddWorkerWindow);
	cmenuBar.btnWorker.addEventListener(MouseEvent.CLICK, showWorkersWindow);
	
	workRecordDragWindow.x = 0;
	workRecordDragWindow.y = 0;
	workRecordDragWindow.visible=false;
	workSpace.addChild(workRecordDragWindow);
	
	workRecordOfWorker.x = 0;
	workRecordOfWorker.y = 0;
	workRecordOfWorker.visible = false;
	workSpace.addChild(workRecordOfWorker);
	
	statisticsSearchWindow.x = 300;
	statisticsSearchWindow.y = 20;
	statisticsSearchWindow.visible = false;
	workSpace.addChild(statisticsSearchWindow);
	statisticsInfoWin.x = 0; 
	statisticsInfoWin.y = 0;
	statisticsInfoWin.width = this.width;
	statisticsInfoWin.height = this.height;
	statisticsInfoWin.visible = false;
	this.addChild(statisticsInfoWin);
	statisticsInfoWin.statisticsInfoGrid.width=5000;
}
public function openStatisticsSearchWindow(event:MouseEvent):void
{
	this.closeOtherChild();
	var workerXml:XML = dbEntity.fetchAllworkerWithDefault();
	var worksetXml:XML = dbEntity.fetchAllworksetWithDefault();
	if(workerXml != null)
	{
		statisticsSearchWindow.workers.dataProvider=workerXml.item.@workerName;
	}
	if(worksetXml != null)
	{
		statisticsSearchWindow.worksets.dataProvider=worksetXml.item.@worksetName;
	}
	statisticsSearchWindow.visible=true;
}

public function showModifyWorkerWindow():void
{
	modifyWorker.x = workersWindow.x + workersWindow.width+10;
	modifyWorker.y = 10;
	modifyWorker.visible=true;
}

public function showAddWorkerWindow(event:MouseEvent):void
{
	addWorkerWindow.x = workersWindow.x + workersWindow.width+10;
	addWorkerWindow.y = 10;
	addWorkerWindow.visible=true;
}
public function showWorkersWindow(event:MouseEvent):void
{
	this.closeOtherChild();
	var workersXml:XML = dbEntity.fetchAllworker();
	if(workersXml!=null)
	{
		workersWindow.workerDataGrid.dataProvider = workersXml.item;
	}
	workersWindow.visible=true;
}
public function showData(event:MouseEvent):void
{
	Alert.show(worksetWindow.worksetGrid.selectedItem.@id);
	//worksetWindow.selectedItemValue.text = worksetWindow.worksetGrid.selectedItem.wNames;
}
public function openViewWorkRecordWindow(event:MouseEvent):void
{
	this.closeOtherChild();
	var workerXml:XML = dbEntity.fetchAllworker();
	if(workerXml != null)
	{
		workRecordOfWorker.workerDataGrid.dataProvider = workerXml.item;
	}
	workRecordOfWorker.visible=true;
}
public function openInputWorkRecordWindow(event:MouseEvent):void
{
	this.closeOtherChild();
	var workerXml:XML = dbEntity.fetchAllworker();
	if(workerXml != null)
	{
		workRecordDragWindow.workers.dataProvider = workerXml.item;
	}
	
	var worksetXml:XML = dbEntity.fetchAllworkset();
	if(worksetXml != null)
	{
		workRecordDragWindow.worksets.dataProvider = worksetXml.item;
	}
	workRecordDragWindow.setStyle("background-image","Embed('components/image/yellow.jpg')");
	workRecordDragWindow.workdate.text="";
	workRecordDragWindow.workerId.text="";
	workRecordDragWindow.workerName.text="";
	workRecordDragWindow.worksetId.text="";
	workRecordDragWindow.worksetName.text="";
	workRecordDragWindow.accountOfWork.text="";
	workRecordDragWindow.infomationId.text="";
	workRecordDragWindow.workdate.setStyle("backgroundColor","#ffffff");
	workRecordDragWindow.accountOfWork.setStyle("backgroundColor","#ffffff");
	workRecordDragWindow.workerName.setStyle("backgroundColor","#ffffff");
	workRecordDragWindow.worksetName.setStyle("backgroundColor","#ffffff");
	Application.application.workRecordDragWindow.visible=true;
	//var workRecordXml:XML = Application.application.dbEntity.fetchAllWorkRecord();
	//var workDate:Date = new Date(workRecordXml.item.@workDate);
	//Alert.show(workDate.fullYear+"");
	//Alert.show(workRecordXml.item.@workDate);
}
public function refreshData(event:DataGridEvent):void
{
	//event.itemRenderer.
	//Alert.show(event.columnIndex+""+event.rowIndex);
	//Alert.show("SSS:"+worksetWindow.worksetGrid.selectedItem.@worksetValue);
	//Alert.show(worksetWindow.worksetGrid.selectedItem.wValue);
}
public function exitApp(event:MouseEvent):void
{
	Application.application.close();
}
public function selectWorksets(event:MouseEvent):void
{
	dbEntity.fetchAllworkset();
} 
public function showMe(event:MouseEvent):void
{
	addWorkset.x = worksetWindow.x + worksetWindow.width+10;
	addWorkset.setName.text="";
	addWorkset.setValue.text="";
	addWorkset.visible = true;
}
public function openWorksetWindow(event:MouseEvent):void
{
	this.closeOtherChild();
	worksetWindow.visible=true;
	var xmlStr:XML = dbEntity.fetchAllworkset();
	if(xmlStr != null)
	{
		worksetWindow.worksetGrid.dataProvider = xmlStr.item;
	}
}
public function addWorkSet(event:MouseEvent):void
{
	var worksetName:String = addWorkset.setName.text;
	var worksetValue:String = addWorkset.setValue.text;
	if(worksetName == "")
	{
		mx.controls.Alert.show("工序名称不能为空");
		return;
	}
	if(worksetValue == "")
	{
		mx.controls.Alert.show("工序单值不能为空");
		return;
	}
	var workset:Workset = new Workset();
	workset.setWorksetName(worksetName);
	workset.setWorksetValue(worksetValue);
	var isExist:Boolean = dbEntity.isWorksetExsit(worksetName);
	if(isExist)
	{
		mx.controls.Alert.show("已经存在名为"+worksetName+"的工序，请不要重复添加");
	}
	else
	{
		dbEntity.saveWorkset(workset);
		addWorkset.visible=false;
		var resultStr:XML = dbEntity.fetchAllworkset();
		if(resultStr != null)
		{
			worksetWindow.worksetGrid.dataProvider = resultStr.item;
		}
	}
}
public function closeOtherChild():void
{
	var childs:Array = workSpace.getChildren();
	for(var i:int = 0; i < childs.length; i++)
	{
		childs[i].visible=false;
	}
}
public function initDb():void
{
	var modelManager:ModelManager = new ModelManager();
	modelManager.setDbpath("c:\\balance.db");
	dbEntity.initDB();
	dbEntity.CreateDB();
}
public function openAddWorkSet(event:MouseEvent):void
{
	addWorkset.x=worksetWindow.x + worksetWindow.width+10;
	addWorkset.setName.text="";
	addWorkset.setValue.text="";
	addWorkset.visible = true;
} 
